<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 10475
  Date: 2023/2/21
  Time: 15:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>招聘用户列表</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/lib/layui-v2.6.3/css/layui.css" media="all">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>
<fieldset class="table-search-fieldset">
    <legend>搜索信息</legend>
    <div style="margin: 10px 10px 10px 10px" class="demoTable">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="keyName" id="keyName" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="keyId" id="keyId" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <select id="keyState" class="layui-border" lay-ignore>
                            <option value="">请选择</option>
                            <option value="正常用户">正常用户</option>
                            <option value="异常用户">异常用户</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button  type="button" class="layui-btn" data-type="reload" onclick="select()" id="find"><i class="layui-icon"></i> 搜 索</button>
                </div>
            </div>
        </form>
    </div>
</fieldset>
<table id="demo" lay-filter="test"></table>
<!-- 引入jquery，需要联网才能使用 -->
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/lib/layui-v2.6.3/layui.js"></script>
<%--<script src="${pageContext.request.contextPath}/layui/lay/modules/table.js"></script>--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑状态</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="state">
    <select name="dp" id="select" class="layui-border" lay-ignore>
        <option value=""></option>
    </select>
</script>
<script>
    layui.use(['table','util','form','layer'], function(){
        var table = layui.table;
        var util=layui.util;
        //第一个实例
        table.render({
            elem: '#demo'
            ,height: 'full-20'
            <%--,url: '${pageContext.request.contextPath}/SelectAllCompanyServlet' //数据接口--%>
            ,url:'${pageContext.request.contextPath}/SelectAllUser'
            ,method:'post'
            ,page: true //开启分页
            ,editTrigger: 'dblclick'//双击编辑
            ,id:'companyList'
            ,smartReloadModel:true
            ,cols: [[ //表头
                {type:'checkbox'}
                ,{field: 'userId', title: '账号', width:150, sort: true}
                ,{field: 'name', title: '用户名', width:150, edit: 'text'}
                ,{field: 'age', title: '年龄', width:150, sort: true, edit: 'text'}
                ,{field: 'school', title: '毕业院校', width:150, edit: 'text'}
                ,{field: 'xueLi', title: '学历', width: 150, edit: 'text'}
                ,{field: 'major', title: '专业', width: 150, sort: true, edit: 'text'}
                ,{field: 'tel', title: '电话', width: 150,edit: 'text'}
                ,{field: 'state', title: '状态', width: 150, sort: true}
                ,{field: 'right', title: '操作', width: 150, toolbar:'#barDemo'} //toolbar 是工具栏
            ]]
        });
        table.on('edit(test)', function(obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段

            $.ajax({
                url: '${pageContext.request.contextPath}/EditUser?userId=' +data.userId + "&name=" + data.name + "&age=" + data.age + "&school=" + data.school + "&xueLi=" + data.xueLi + "&major=" + data.major + "&tel=" +  data.tel + "&state=" + data.state,
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                dataType: 'text',
                data: {
                    'userId': data.userId,
                    'name': data.name,
                    'age': data.age,
                    'school': data.school,
                    'xueLi': data.xueLi,
                    'major':data.major,
                    'tel': data.tel,
                    'state': data.state,
                },
                success: function (res) {
                    if("success"==res) {
                        layer.msg('[ID: ' + data.companyId + '] ' + field + ' 字段更改值为：' + value);
                    }else{
                        alert("修改失败失败");
                    }
                },
                error: function (res) {
                    layer.msg("实在抱歉，编辑失败！");
                }
            });
            layer.close(index);//关闭弹窗

        });
        //监听行事件
        //触发单元格工具事件
        table.on('tool(test)', function(obj){ // 双击 toolDouble
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    console.log(data.companyId);
                    $.ajax({
                        type:"post",
                        url:"${pageContext.request.contextPath}/RemoveUser?userId="+data.userId,
                        contentType: 'application/json;charset=utf-8',
                        dataType:"text",
                        success:function(res){
                            console.log(res);
                            layer.msg("删除成功！");
                        },
                        error:function(res){
                            layer.msg("删除失败！");
                        }
                    });
                    layer.close(index);
                });
            }else if(obj.event === 'edit'){
                //实现编辑功能
                //打开弹出层
                layer.open({
                    type:2
                    ,title:'编辑求职者信息'
                    ,content:'${pageContext.request.contextPath}/jsp/Admin/editUserState.jsp'
                    //是否最大化
                    ,maxmin:true
                    ,area:['500px','450px']
                    ,btn:['确定','取消']
                    //回调函数
                    //参数 索引和layero
                    ,yes:function (index,layero) {
                        //回调的数值
                        var userId=$(layero).find('iframe')[0].contentWindow.userId.value;
                        var state=$(layero).find('iframe')[0].contentWindow.document.getElementById('state').value;
                        console.log(userId);
                        console.log(state);

                        //同步数据表格中的数据
                        obj.update({
                            'state':state,
                            'userId':userId,
                        })

                        //	<!--在线引入jquery后正常使用 -->
                        $.ajax({
                            url:'${pageContext.request.contextPath}/EditUserState?userId='+userId+"&state="+state,
                            type:'post',
                            contentType:'application/json;charset=utf-8',
                            dataType:'text',
                            data:{
                                'userId':userId,
                                'state':state,
                            },
                            success:function(res){
                                if("success"==res){
                                    alert("恭喜你，编辑成功！")
                                }else {
                                    layer.msg("实在抱歉，编辑失败！");
                                }

                            },
                            error:function(res){
                                layer.msg("实在抱歉，编辑失败！");
                            }
                        })
                        layer.close(index);//关闭弹窗
                    },success:function (layero,index) {
                        var div = layero.find('iframe').contents().find('#useradmin');
                        var body = layer.getChildFrame('body',index);
                        var iframeWindow=window['layui-layer-iframe'+index];
                        //获取未修改前的默认数值到编辑框中，方便用户修改自己想修改的部分
                        body.find('#userId').val(data.userId);
                        body.find('#state').val(data.state);

                    }
                })

            }

        });
    });

</script>
<script type="text/javascript">
    function select() {
        var keyName = $('#keyName').val();
        var keyId = $('#keyId').val();
        var keyState = $('#keyState').val();
        console.log(keyState);
        var table = layui.table;
        layui.table.reload('companyList', {
            page:{
                curr:1  //从第一页开始
            }
            ,page: true //开启分页
            ,method:'post'
            ,url:'${pageContext.request.contextPath}/SelectAllUser?keyName='+keyName+"&keyId="+keyId+"&keyState="+keyState,
            where:{
                keyName:keyName,
                keyId:keyId,
                keyState:keyState
            }
        });
        console.log(keyName);
    }

</script>
</body>
</html>